//
//  WordListViewController.m
//  Learn
//
//  Created by apple on 2024/4/3.
//  Copyright © 2024 迟建国. All rights reserved.
//

#import "WordListViewController.h"
#import "WordListItemView.h"

@interface WordListViewController ()<UIScrollViewDelegate>
/* <#描述#> */
@property (nonatomic, strong) UIButton    * leftBtn;
@property (nonatomic, strong) UIButton    * rightBtn;

@property (nonatomic, strong) UIView    * indicatorView;

@property (nonatomic, strong) UIScrollView    * myScrollView;//

@property (nonatomic, assign) BOOL            isClickedSegmented;       //
@property (nonatomic, assign) NSInteger            startPage;

/* <#描述#> */
@property (nonatomic, strong) WordListItemView    * yixueList;
@property (nonatomic, strong) WordListItemView    * weixueList;

@end

@implementation WordListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = UIColor.whiteColor;
    self.title = @"单词列表";

    [self.view addSubview:self.leftBtn];
    [self.view addSubview:self.rightBtn];
    [self.view addSubview:self.indicatorView];
    [self.view addSubview:self.myScrollView];
}
- (UIButton *)leftBtn {
    if(!_leftBtn){
        _leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _leftBtn.frame = CGRectMake(0, NavigationBarHeight, 72, 45);
        _leftBtn.right = SCREEN_W/2 - 30;
        [_leftBtn setTitle:@"已学单词" forState:UIControlStateNormal];
        
        [_leftBtn setTitleColor:COLOR_BLACK forState:UIControlStateNormal];
        [_leftBtn setTitleColor:COLOR_TOPIC_SELECT forState:UIControlStateSelected];
        _leftBtn.selected = YES;
        _leftBtn.titleLabel.font  = PingFangMediumFont(18);
        [_leftBtn addTarget:self action:@selector(leftBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _leftBtn;
}
- (UIButton *)rightBtn {
    if(!_rightBtn){
        _rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _rightBtn.frame = CGRectMake(self.leftBtn.right + 60, self.leftBtn.top, self.leftBtn.width, self.leftBtn.height);
        
        [_rightBtn setTitle:@"未学单词" forState:UIControlStateNormal];
        [_rightBtn setTitleColor:COLOR_BLACK forState:UIControlStateNormal];
        [_rightBtn setTitleColor:COLOR_TOPIC_SELECT forState:UIControlStateSelected];

        _rightBtn.titleLabel.font  = PingFangMediumFont(18);
        [_rightBtn addTarget:self action:@selector(rightBtnClicked:) forControlEvents:UIControlEventTouchUpInside];

    }
    return _rightBtn;
}

- (UIView *)indicatorView {
    if(!_indicatorView){
        _indicatorView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 28, 3)];
        _indicatorView.centerX = self.leftBtn.centerX;
        _indicatorView.bottom = self.leftBtn.bottom;
        _indicatorView.backgroundColor = COLOR_TOPIC_SELECT;
        _indicatorView.layer.cornerRadius = 1.5;
        _indicatorView.layer.masksToBounds = YES;
    }
    return _indicatorView;
}
- (UIScrollView *)myScrollView{
    if (!_myScrollView)
    {
        _myScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, self.indicatorView.bottom, SCREEN_W,SCREEN_H - self.indicatorView.bottom)];
        _myScrollView.delegate = self;
        _myScrollView.backgroundColor = COLOR_APP_BACKGROUND;
        _myScrollView.pagingEnabled = YES;
        _myScrollView.contentSize = CGSizeMake(SCREEN_W * 2, CGRectGetHeight(_myScrollView.frame));
        [_myScrollView addSubview:self.yixueList];
        [_myScrollView addSubview:self.weixueList];
    }
    return _myScrollView;
}

- (WordListItemView *)yixueList {
    if (!_yixueList) {
        _yixueList = [[WordListItemView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_W, self.myScrollView.height) type:1];
        _yixueList.baseVC = self;
    }
    return _yixueList;
}
- (WordListItemView *)weixueList {
    if (!_weixueList) {
        _weixueList = [[WordListItemView alloc]initWithFrame:CGRectMake(SCREEN_W, 0, SCREEN_W, self.myScrollView.height) type:2];
        _weixueList.baseVC = self;
    }
    return _weixueList;
}



#pragma mark - ----------------------- 代理 -----------------------
#pragma mark 解决Segmented的滑块快速滑动时的延迟，同时把点击滑块的情况排除在外
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (!self.isClickedSegmented)
    {
        CGFloat pageWidth = scrollView.frame.size.width;
        NSInteger tmpPage = scrollView.contentOffset.x / pageWidth;
        float tmpPage2 = scrollView.contentOffset.x / pageWidth;
        NSInteger page = tmpPage2-tmpPage>=0.5 ? tmpPage+1 : tmpPage;
        if (_startPage != page)
        {
            _startPage = page;
            self.indicatorView.centerX = _startPage == 0? self.leftBtn.centerX : self.rightBtn.centerX;
        }
    }
}
     //
#pragma mark 页面滚动，同时调起Segmented的滑块滑动起来等
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    CGFloat pageWidth = scrollView.frame.size.width;
    NSInteger page = scrollView.contentOffset.x / pageWidth;
    _startPage = page;
    
    self.isClickedSegmented = NO;
    self.indicatorView.centerX = _startPage == 0? self.leftBtn.centerX : self.rightBtn.centerX;
    
}

#pragma mark   点击
- (void)leftBtnClicked:(UIButton *)sender {
    if (!self.leftBtn.selected) {
        self.leftBtn.selected = YES;
        self.rightBtn.selected = NO;
        self.indicatorView.centerX = self.leftBtn.centerX;

    }
}
- (void)rightBtnClicked:(UIButton *)sender {
    if (!self.rightBtn.selected) {
        self.rightBtn.selected = YES;
        self.leftBtn.selected = NO;
        self.indicatorView.centerX = self.rightBtn.centerX;

    }
}
@end
